package main

import (
	"config-client-v1/model"
	"config-client-v1/utils/tools"
	"fmt"
)

type ZUser struct {
	ID         int            `json:"id"`
	Name       string         `json:"name"`
	Code       string         `json:"code"`
	CreateTime *string        `json:"create_time"`
	UpdateTime *string        `json:"update_time"`
	Cards      []*ZCreditCard `json:"cards"`
}
type ZCreditCard struct {
	ID         int     `json:"id"`
	UserID     int     `json:"user_id"`
	Bank       string  `json:"name"`
	CardNo     string  `json:"code"`
	CreateTime *string `json:"create_time"`
	UpdateTime *string `json:"update_time"`
}

func hasOne() {
}

func hasMany() {
	now := tools.GetNow()
	user := &ZUser{}
	user.Name = "关羽"
	user.Code = "0002"
	user.CreateTime = &now
	user.Cards = make([]*ZCreditCard, 0)
	user.Cards = append(user.Cards, &ZCreditCard{
		Bank:       "招商银行",
		CardNo:     "050410",
		CreateTime: &now,
		UpdateTime: &now,
	})
	user.Cards = append(user.Cards, &ZCreditCard{
		Bank:       "工商银行",
		CardNo:     "110001",
		CreateTime: &now,
		UpdateTime: &now,
	})
	err := model.DB.Omit("Cards").Create(user).Error
	//err := model.DB.Omit("Cards").Where("id = ?", 1).Select("*").Updates(user).Error
	if err != nil {
		fmt.Println("sql error", err)
	} else {
		fmt.Println("sql success", user.ID)
		for _, row := range user.Cards {
			row.UserID = user.ID
		}
		err = model.DB.Create(user.Cards).Error
		if err != nil {
			fmt.Println("sql2 error", err)
		} else {
			fmt.Println("sql2 success", len(user.Cards))
		}
	}
}

func main() {
	hasMany()
}
